package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jackie.tongzhuang.Adapters.FenQiCardAdapter;
import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Classes.BusinessClass;
import com.jackie.tongzhuang.Controls.GridRadioGroup;
import com.jackie.tongzhuang.Controls.OptionItem;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Enums.QiShu;
import com.jackie.tongzhuang.Models.FenQi;
import com.jackie.tongzhuang.Models.JinHuoDan;
import com.jackie.tongzhuang.Models.YinHangKa;

import java.util.ArrayList;
import java.util.List;

public class InsertFenQiActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView zjetext,kyedtext,xyyhtext;
    private GridRadioGroup group;
    private ListView fqlist;
    private JinHuoDan jhd;
    private TitleBar titleBar;

    private YinHangKa yhk;
    private DbClass dbClass = new DbClass(this);
    private int fqs = 1;
    private List<FenQi> fqls = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_fenqi);
        Intent intent = getIntent();
        jhd = (JinHuoDan) intent.getSerializableExtra("jinhuodan");
        yhk = dbClass.getYinHangKa(1);


        initView();

        group.setColumnNum(3);

        //String _zje = BaseClass.getDecimalToStr(2,BaseClass.getDecimal(2,jhd.getJinHuoJinE()));
        String _zje = BaseClass.getDecimalToStr(2,jhd.getJinHuoJinE());
        zjetext.setText("¥"+_zje);
        String _kyed = BaseClass.getDecimalToStr(2,yhk.getKeYongEDu());
        kyedtext.setText("¥"+_kyed);


        String xqrq = BaseClass.getNextFenQiData(1);
        String _fqzje = BaseClass.getDecimalToStr(2,dbClass.getXiaQiYingHuan(xqrq));
        xyyhtext.setText("¥"+_fqzje);

        initOption();


        if(group.getChildCount() > 0) {
            OptionItem oi = (OptionItem) group.getChildAt(0);
            oi.setSelected(1);
        }


        titleBar.setHideSubmit(false);
        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }
            @Override
            public void submitClick(View v) {
                saveFenQi();
            }
        });

    }

    private void initView(){
        zjetext = findViewById(R.id.fq_add_zje);
        group = findViewById(R.id.fq_add_fqgroup);
        fqlist = findViewById(R.id.fq_add_fqlist);
        kyedtext = findViewById(R.id.fq_add_kyed);
        xyyhtext = findViewById(R.id.fq_add_xyyh);
        titleBar = findViewById(R.id.fq_add_titlebar);

    }

    private void initOption(){
        for (QiShu qs : QiShu.values()) {
            OptionItem qo = new OptionItem(this,null);
            qo.setValue(qs.getValue());
            qo.setText(qs.getText());
            qo.setOnSelectedListener(new OptionItem.OnSelectedListener() {
                @Override
                public void onSelected(int sel_val) {
                    fqs = sel_val;
                    if(sel_val != -1){
                        //开始计算分期数据，并显示在listView中
                        fqls = BusinessClass.getFenQi(Double.valueOf(jhd.getJinHuoJinE()),fqs);
                        FenQiCardAdapter fqcAdapter = new FenQiCardAdapter(InsertFenQiActivity.this,fqls);
                        fqlist.setAdapter(fqcAdapter);
                    }
                }
            });
            LinearLayout.LayoutParams paramsRb = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            group.addView(qo, paramsRb);
        }
    }


    @Override
    public void onClick(View view) {

    }

    private void saveFenQi(){
        if(fqls.size() == 0){
            Toast.makeText(this,"请选择分期",Toast.LENGTH_SHORT).show();
            return;
        }

        long jhdh = 0;
        //添加进货单
        ContentValues jhd_values = new ContentValues();
        jhd_values.put("LeiBieId",jhd.getLeiBieId());
        jhd_values.put("GongHuoShangId",jhd.getGongHuoShangId());
        jhd_values.put("JinHuoRiQi",jhd.getJinHuoRiQi());
        jhd_values.put("JinHuoJinE",jhd.getJinHuoJinE());
        jhdh = dbClass.insert("JinHuoDan",jhd_values);

        //添加分期数据
        for(FenQi fq : fqls){
            ContentValues fq_values = new ContentValues();
            fq_values.put("JinHuoDanId",(int)jhdh);
            fq_values.put("QiShu",jhdh+"-"+fq.getQiShu());
            fq_values.put("YiChangHuan",fq.getYiChangHuan());
            fq_values.put("FenQiJinE",fq.getFenQiJinE());
            fq_values.put("BenJin",fq.getBenJin());
            fq_values.put("LiXi",fq.getLiXi());
            fq_values.put("FenQiRiQi",fq.getFenQiRiQi());
            fq_values.put("ChangHuanRiQi",fq.getChangHuanRiQi());
            dbClass.insert("FenQi",fq_values);
        }

        //更改银行卡可用额度
        double kyEDu = Double.valueOf(kyedtext.getText().toString().split("¥")[1]);
        double jhEDu = Double.valueOf(zjetext.getText().toString().split("¥")[1]);
        ContentValues yhk_values = new ContentValues();
        yhk_values.put("KeYongEDu",kyEDu - jhEDu);
        dbClass.update("YinHangka",yhk_values,1);

        finish();
    }
}